<?php 
class Form
{	
	/**
	*	author: Lukasz Kowalski
	*	contact:wokiee@wokiee.com
	*	request author permmision before you use this class!
	*	this class allow to create custom form in few seconds provides a full form inputs and buttons
	*/

	/* vars */
	protected $pCount=0; /* pointer for array, array contains generated form*/
	
	/* methods */
	function __construct($url, $method){
		/**
		*	@arg String $url, String $method
		*	Constructor make custom form init, $url sets form action, $method sets form submiting method ,always use method endForm after setup form
		*/
		$this->gen[$this->pCount]='<form action="'.$url.'" method="'.$method.'" enctype="multipart/form-data">'."\n";
		$this->pCount++;
	}
	
	function textInput($name,$title,$class){
		/**	
		*	@arg String $name contains name and id for text input and  name for label
		*	@arg String $title contains title of text input box 
		*	@arg String $class contains class  name for label (null sets no css class)
		*	Functionn adds new text input to form
		*/
		if( $class == null){ $class = ''; }
		else {$class = 'class="'.$class.'" ';}
		$id=$name;
		$this->gen[$this->pCount] = '<label '.$class.'for="'.$id.'">'.$title.'</label>'."\n";
		$this->gen[$this->pCount] .= '<input type="text" name="'.$name.'" id="'.$id.'" /><br />'."\n";
		$this->pCount++;
	}
	function passInput($name,$title,$class){
		/**	
		*	@arg String $name contains name and id for text input and  name for label
		*	@arg String $title contains title of text input box 
		*	@arg String $class contains class  name for label (null sets no css class)
		*	Functionn adds new text input to form
		*/
		if( $class == null){ $class = ''; }
		else {$class = 'class="'.$class.'" ';}
		$id=$name;
		$this->gen[$this->pCount] = '<label '.$class.'for="'.$id.'">'.$title.'</label>'."\n";
		$this->gen[$this->pCount] .= '<input type="password" name="'.$name.'" id="'.$id.'" /><br />'."\n";
		$this->pCount++;
	}
	function startFieldset($label){
		/**	
		*	@arg String $label contains legend for fieldset
		*	Functionn adds new fieldset to form, method end Fieldsed are closing form
		*/
		if($label == null){ $label = ''; }
		else {$legend = '<legend>'.$label.'</legend>';}
		$this->gen[$this->pCount] = '<fieldset>'."\n";
		$this->gen[$this->pCount] .= $legend."\n";
		$this->pCount++;
	}
	
	function endFieldset(){
		/**		
		*	Functionn ends fieldset in form use always after
		*/
		$this->gen[$this->pCount] = '</fieldset>'."\n\n";
		$this->pCount++;
	}
	
	function newline(){
		/**		
		*	Functionn add <br /> to form
		*/
		$this->gen[$this->pCount] = '<br />'."\n";
		$this->pCount++;
	}
	
	function checkBox($type,$name,$value,$title){
		/**		
		*	Function add checkbox to form
		* 	$type contains checkboxtype (radio or checkbox)
		*	$name contains name of group checkbox
		*	$value contains  value to select
		*	$title contaains text displayed after checkbox
		*/
		$id=$value.$name;
		if($type=='checkbox'){
			$this->gen[$this->pCount] = '<input type="checkbox" name="'.$name.'" id="'.$id.'" value="'.$value.'" />'."\n";
			$this->gen[$this->pCount] .='<label for="'.$id.'">'.$title.'</label><br />'."\n";
			$this->pCount++;
		}
		if($type=='radio'){
			$this->gen[$this->pCount] = '<input type="radio" name="'.$name.'" id="'.$id.'" value="'.$value.'" />'."\n";
			$this->gen[$this->pCount] .='<label for="'.$id.'">'.$title.'</label>'."\n";
			$this->pCount++;	
		}
		else {return false;}
	}
	
	function fileInput($name,$title){
		/**		
		*	Method allow to submit a file 
		*	$name contains filename
		*	$title contains Input title
		*/
		$this->gen[$this->pCount] = '<label for="'.$name.'">'.$title.'</label><br />'."\n";
		$this->gen[$this->pCount] .= '<input type="file" name="'.$name.'" id="'.$name.'" /><br />'."\n";
		$this->pCount++;	
	}
	
	function textarea($name,$title,$class,$rows,$cols){
		/**		
		*	Method allow to create textarea colls and rows default empty
		*	$name contains name of textarea
		*	$title contains textarea title
		*	$class add class to label
		*/
		if( $class == null){ $class = ''; }
		else {$class = 'class="'.$class.'" ';}
		$id=$name;
		$this->gen[$this->pCount] = '<label '.$class.' for="'.$name.'">'.$title.'</label>'."\n";
		$this->gen[$this->pCount] .= '<textarea name="'.$name.'" id="'.$name.'" rows="'.$rows.'" cols="'.$cols.'"> </textarea><br />'."\n";
		$this->pCount++;	
	}
	
	function startSelect($name,$title,$class){
		/**
		*	Method create select box in form
		*	@arg String $name contains select name 
		*	@arg String $title contains select box title
		*	@arg String $class contains class of label. null = no class
		*	After use remember use endSelect() method!
		*/
		if( $class == null){ $class = ''; }
		else {$class = 'class="'.$class.'" ';}
		$id=$name;
		$this->gen[$this->pCount] = '<label '.$class.'for="'.$id.'">'.$title.'</label>'."\n";
		$this->gen[$this->pCount] .= '<select name="'.$name.'" id="'.$id.'">'."\n";
		$this->pCount++;
	} 
	
	function endSelect(){
		/**
		*	Method closes select box in form
		*/
		$this->gen[$this->pCount] = '</select><br />'."\n\n";
		$this->pCount++;
	}
	
	function addSelectOption($value, $display){
		/**
		*	Method adds option into select box
		*	@arg String $value contains select option value
		*	@arg String $display contains selected option message
		*/
		$this->gen[$this->pCount] = '<option value="'.$value.'">'.$display.'</option>'."\n";
		$this->pCount++;
	}
	
	function customAddForm($any){
		/**
		*	Function allow to add custom code into form
		*/
		$this->gen[$this->pCount]=$any."\n";
		$this->pCount++;
	}
	
	
	function submitForm(){
		/**
		* 	Function adds submitButton to form
		*/
		$this->gen[$this->pCount]='<input type="submit" />'."\n";
		$this->pCount++;
	}

	function resetForm(){
		/**
		* 	Function adds resetButton to form
		*/
		$this->gen[$this->pCount]='<input type="reset" />'."\n";
		$this->pCount++;
	}
	
	function endForm(){
		/**
		* 	Function adds </form> to form
		*/
		$this->gen[$this->pCount]='</form>'."\n";
		$this->pCount++;
	}

	function showForm(){
		/**
		*	Function shows maked form
		*/
		foreach ($this->gen as $out)
		echo $out;
	}
};

?>
